import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)
// 防止路由重复点击报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
const routes = [
  {
    path:'/',
    redirect:'/login'
  },
  {
    path:'/login',
    name:'Login',
    component:()=>import('@/views/login')
  },
  {
    path:'/register',
    name:'Register',
    component:()=>import('@/views/register')
  },
  {
    path:'/manager',
    name:'Manager',
    component:()=>import('@/views/manager'),
    children:[
      {
        path:'/manager',
        redirect:'/manager/home'
      },
      {
        path:'home',
        name:'Home',
        component:()=>import('@/views/Home/Home.vue')
      },
      {
        path:'mine',
        name:'Mine',
        component:()=>import('@/views/mine/mine.vue')
      },
      {
        path:'earning',
        name:'Earning',
        component:()=>import('@/views/earning/earning.vue')
      },
      {
        path:'order',
        name:'Order',
        component:()=>import('@/views/order/order.vue')
      },
      {
        path:'detail',
        component:()=>import('@/views/order/detail')
      }
    ]
  },
  //实名页面
  {
    path:'/authentication',
    name:'Authentication',
    component:()=>import('@/views/mine/authentication')
  },
  //个人详情页面
  {
    path:'/authentication_details',
    component:()=>import('@/views/mine/authentication_details')
  },

]

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由守卫 判断有无token
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
  //若要跳转的页面是登录界面
  if (to.path === '/login') {
    //直接跳转
    next();
  } else { //若想要跳转其他页面
    //获取本地存储的token值
    let token = localStorage.getItem('token');
    //若token为空则验证不成功，跳转到登录页面
    if (token === 'null' || token === '') {
      next('/login');
    } else {
      next();
    }
  }
});

export default router
